package com.kmxd.ams.app.use;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.kmxd.ams.client.archive.IArchiveTypeSvc;
import com.kmxd.ams.client.archive.vo.ArchiveTypeVO;
import com.kmxd.ams.client.arrange.IDocumentSvc;
import com.kmxd.ams.client.arrange.IVolumeSvc;
import com.kmxd.ams.client.use.IUseArchSvc;
import com.kmxd.ams.client.use.dto.UseArchAddDTO;
import com.kmxd.ams.client.use.dto.UseArchEditDTO;
import com.kmxd.ams.client.use.dto.UseArchQueryDTO;
import com.kmxd.ams.client.use.vo.UseArchListVO;
import com.kmxd.ams.client.use.vo.UseArchVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.archive.entity.ArchiveType;
import com.kmxd.ams.infra.use.entity.UseArch;
import com.kmxd.ams.infra.use.mapper.UseArchMapper;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 全宗|单位利用档案 服务实现类
 *
 * @author ZuoLG
 * @since 2022-08-02
 */
@Service
@AllArgsConstructor
public class UseArchSvcImpl extends ServiceImpl<UseArchMapper, UseArch> implements IUseArchSvc {

  private UseArchMapper useArchMapper;

  private IDocumentSvc documentSvc;
  private IVolumeSvc volumeSvc;

  @Override
  public Page<UseArchListVO> selectPage(Page reqPage, UseArchQueryDTO req) {
    LambdaQueryWrapper<UseArch> queryWrapper =
        QueryGen.init(new QueryWrapper<UseArch>(), req).lambda();
    Page<UseArch> page = useArchMapper.selectPage(reqPage, queryWrapper);
    Page<UseArchListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), UseArchListVO.class));
    return voPage;
  }

  @Override
  public List<UseArchListVO> selectList(UseArchQueryDTO req) {
    LambdaQueryWrapper<UseArch> queryWrapper =
        QueryGen.init(new QueryWrapper<UseArch>(), req).lambda();
    List<UseArch> list = useArchMapper.selectList(queryWrapper);
    return convert(list);
  }

  public List<UseArchListVO> convert(List<UseArch> list) {
    if (ObjectUtil.isEmpty(list)) {
      return Lists.newArrayList();
    }
    IArchiveTypeSvc archiveTypeSvc = SpringUtil.getBean(IArchiveTypeSvc.class);
    List<Long> archTypeIds =
        list.stream().map(UseArch::getArchTypeId).distinct().collect(Collectors.toList());
    List<ArchiveType> archiveTypes =
        Optional.ofNullable(archiveTypeSvc.listByIds(archTypeIds)).orElse(Lists.newArrayList());
    Map<Long, ArchiveType> archMap =
        archiveTypes.stream().collect(Collectors.toMap(ArchiveType::getId, Function.identity()));
    return list.stream()
        .map(
            item -> {
              UseArchListVO vo = BeanUtil.copyProperties(item, UseArchListVO.class);
              ArchiveType archiveType = archMap.get(vo.getArchTypeId());
              ArchiveTypeVO archiveTypeVO = BeanUtil.copyProperties(archiveType, ArchiveTypeVO.class);
              vo.setArchiveType(archiveTypeVO);
              return vo;
            })
        .collect(Collectors.toList());
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(UseArchAddDTO req) {
    UseArch entity = BeanUtil.copyProperties(req, UseArch.class);
    useArchMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(UseArchEditDTO req) {
    UseArch entity = BeanUtil.copyProperties(req, UseArch.class);
    useArchMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public UseArchVO view(Long id) {
    UseArch entity = useArchMapper.selectById(id);
    return BeanUtil.copyProperties(entity, UseArchVO.class);
  }
}
